package com.quanshu.shop.pay.custom;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.jungly.gridpasswordview.GridPasswordView;
import com.quanshu.exchange.R;
import com.quanshu.exchange.support.net.BaseResponse;
import com.quanshu.exchange.support.net.core.BaseObserver;
import com.quanshu.exchange.support.net.core.BaseTransformer;
import com.quanshu.exchange.support.net.core.NetClient;
import com.quanshu.exchange.support.utils.RxViewHelper;
import com.quanshu.exchange.ui.base.BaseActivity;
import com.quanshu.exchange.ui.base.MyApplication;
import com.quanshu.shop.main.api.ShopMainIF;
import com.quanshu.shop.order.entity.CreatOrderEntity;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 作者  李哲
 * 创建时间 2018/8/3
 * 邮箱 lizhetongliao@163.com
 * 说明
 **/
public class PayPasswordDialog extends Dialog {
    @BindView(R.id.pay_dialog_txt1)
    TextView payDialogTxt1;
    @BindView(R.id.pay_dialog_pwd)
    GridPasswordView payDialogPwd;
    @BindView(R.id.pay_dialog_ok)
    TextView payDialogOk;
    @BindView(R.id.balance_fl_close)
    FrameLayout balanceFlClose;
    @BindView(R.id.pay_dialog_tips)
    TextView payDialogTips;
    private Context mContext;
    private OnPayListener onPayListener;
    private int mCount = 0;
    private String mPwd;
    private CreatOrderEntity creatOrderEntity;
    private String njfNumber;

    public PayPasswordDialog(Context context) {
        super(context, R.style.CartDialogWindow);
        mContext = context;
    }

    public void show(CreatOrderEntity creatOrderEntity1, String njfNumber1) {
        creatOrderEntity = creatOrderEntity1;
        njfNumber = njfNumber1;
        super.show();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_pay_pwd);
        ButterKnife.bind(this);
        setCancelable(true);//点击外部不可dismiss
        setCanceledOnTouchOutside(true);
        Window window = this.getWindow();
        window.setGravity(Gravity.CENTER);
        WindowManager.LayoutParams params = window.getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
        initView();
    }

    private void initView() {
        RxViewHelper.getInstance().clicks(balanceFlClose, aVoid ->
                dismiss()
        );
        RxViewHelper.getInstance().clicks(payDialogOk, aVoid ->
                {
                    mCount++;
                    payDialogOk.setEnabled(false);
                    TestPassword(mPwd);

                }

        );
        payDialogPwd.setOnPasswordChangedListener(new GridPasswordView.OnPasswordChangedListener() {
            @Override
            public void onTextChanged(String psw) {
                Log.d("正在输入", "onTextChanged: ");
                payDialogTips.setText("");
                payDialogTips.setVisibility(View.INVISIBLE);
                if (psw.length() < 6) {
                    payDialogOk.setEnabled(false);
                } else {
                    payDialogOk.setEnabled(true);
                }
            }

            @Override
            public void onInputFinish(String psw) {
                Log.d("输入完成", "onTextChanged: ");
                mPwd = psw;
            }
        });
    }


    public PayPasswordDialog setPayListener(OnPayListener onPayListener1) {
        onPayListener = onPayListener1;
        return this;
    }

    private void TestPassword(String pwd) {
        NetClient.getInstance().getShopService(ShopMainIF.class).doNjfPay(MyApplication.getInstance().getCurrentUser().getId(), creatOrderEntity.getOrder_no(), creatOrderEntity.getNiuScore_sum(), pwd, MyApplication.getInstance().getCurrentUser().getMemberid())
                .compose(new BaseTransformer<>((BaseActivity) mContext)).subscribe(new BaseObserver<BaseResponse<String>>() {
            @Override
            public void onComplete() {
                payDialogOk.setEnabled(true);
            }

            @Override
            public void onFailure(Throwable e) {
                payDialogTips.setText(e.getMessage());
                payDialogTips.setVisibility(View.VISIBLE);
                if (mCount >= 3) {
                    if (onPayListener != null) {
                        onPayListener.isSuccessPay(false);
                    }
                    dismiss();
                }
                payDialogPwd.clearPassword();
            }

            @Override
            public void onSuccess(BaseResponse<String> response) {
                if (onPayListener != null) {
                    onPayListener.isSuccessPay(true);
                }
            }
        });
    }

    public interface OnPayListener {
        void isSuccessPay(boolean is);
    }
}
